package com.apanal.qlife.monitor.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;

/**
 * 在线用户监控控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-10-19下午2:39:26
 */
@Controller
@RequestMapping("/monitor/online")
public class UserOnlineMonitorController {
	@Autowired
	private SessionDAO sessionDAO;

	/**
	 * 登录用户列表
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "在线用户监控", operation = "查询在线用户", type = LogType.SELECT)
	@RequiresPermissions("monitor:online:view")
	@RequestMapping()
	public String list(Model model) {
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		List<Session> show = new ArrayList<Session>();
		for (Session session : sessions) {
			// 如果session用户身份为空,则该用户未登录,移除(用户刚进入登录界面就会产生session)
			if (session != null
					&& session
							.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) != null) {
				show.add(session);
			}
		}
		// 登录时间倒序
		Collections.sort(show, new Comparator<Session>() {
			@Override
			public int compare(Session o1, Session o2) {
				return -o1.getStartTimestamp()
						.compareTo(o2.getStartTimestamp());
			}
		});
		model.addAttribute("sessions", show);
		model.addAttribute("sessionCount", show.size());
		return "monitor/online/index";
	}

	/**
	 * 强制退出用户
	 * 
	 * @param sessionId
	 * @return
	 */
	@SystemLog(module = "在线用户监控", operation = "强制退出用户", type = LogType.DELETE)
	@RequiresPermissions("monitor:online:delete")
	@ResponseBody
	@RequestMapping("/{sessionId}/forceLogout")
	public String forceLogout(@PathVariable("sessionId") String sessionId) {
		try {
			Session session = sessionDAO.readSession(sessionId);
			if (session != null) {
				session.setAttribute(Constants.SESSION_FORCE_LOGOUT_KEY,
						Boolean.TRUE);
			}
		} catch (Exception e) {/* ignore */
		}
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 后台登录用户在线人数
	 * 
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("sysUserCount")
	public int sysUserCount() {
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		Collection<Session> show = new ArrayList<Session>();
		for (Session session : sessions) {
			// 如果session用户身份为空,则该用户未登录,移除(用户刚进入登录界面就会产生session)
			if (session
					.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) != null) {
				show.add(session);
			}
		}
		return show.size();
	}

}
